package com.ai.controller;

import com.ai.common.KafkaConstant;
import com.ai.entity.dto.Result;
import com.ai.exception.AiException;
import com.ai.service.CallService;
import com.ai.util.ReadOrWriteFileUtil;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 41636
 */
@RestController
@RequestMapping("/basic")
public class BasicController {

    @Resource
    private CallService callService;
    @Resource
    private KafkaTemplate<String, String> kafkaTemplate;

    @GetMapping("/{itemName}")
    public Result get(@PathVariable("itemName") String itemName) {
        callService.exec(itemName);
        return Result.ok();
    }

    @GetMapping("/A/{itemName}")
    public Result singleA(@PathVariable("itemName") String itemName) {
        ReadOrWriteFileUtil.deleteLikeFile("A", itemName);
        callService.singleA(itemName);
        return Result.ok();
    }

    @GetMapping("/B/{itemName}")
    public Result singleB(@PathVariable("itemName") String itemName) {
        ReadOrWriteFileUtil.deleteLikeFile("B", itemName);
        ReadOrWriteFileUtil.deleteLikeFile("C", itemName);
        callService.singleB(itemName);
        return Result.ok();
    }

    @GetMapping("/C/{itemName}")
    public Result singleC(@PathVariable("itemName") String itemName) {
        ReadOrWriteFileUtil.deleteLikeFile("D", itemName);
        callService.singleC(itemName);
        return Result.ok();
    }

    @GetMapping("/submit")
    public Result submit() {
        List<String> itemNames = ReadOrWriteFileUtil.readTxtLines("disease", "itemNames");
        for (String itemName : itemNames) {
            kafkaTemplate.send(KafkaConstant.AI_ITEM_NAME, itemName);
        }
        return Result.ok();
    }
}
